package com.javasm.sys.controller;

import com.javasm.commons.base.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.entity.TableDatas;
import com.javasm.sys.SysTimingTask;
import com.javasm.sys.TasksService;
import com.javasm.sys.task.AsyncManager;
import com.javasm.sys.task.CrmTimingTask;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;


@RestController
@RequestMapping("tasks")
public class TimingTaskController extends BaseController {
    @Resource
    private TasksService tasksService;
    @Resource
    private CrmTimingTask crmTimingTask;

    @GetMapping
    public AxiosResult getTasks(SysTimingTask sysTimingTask) {
        startPage();
        List<SysTimingTask> list = tasksService.getTasks(sysTimingTask);
        return AxiosResult.suc(new TableDatas(list));
    }
    @PostMapping
    public AxiosResult addTasks(@RequestBody SysTimingTask sysTimingTask) {
        Integer flag = tasksService.addTask(sysTimingTask);
        return isok(flag == 1);
    }
    @PutMapping
    public AxiosResult updateTasks(@RequestBody SysTimingTask sysTimingTask) {
        Integer flag = tasksService.updateTaskById(sysTimingTask);
        return isok(flag == 1);
    }
    @DeleteMapping("{taskId}")
    public AxiosResult delTask(@PathVariable String taskId) {
        Integer flag = tasksService.delTaskById(taskId);
        return isok(flag == 1);
    }

    @GetMapping("executeOne/{taskid}")
    public AxiosResult executeOneTask(@PathVariable String taskid){
        SysTimingTask t = tasksService.getById(taskid);
        String taskStatus = t.getTaskState();
        if("running".equals(taskStatus)) return AxiosResult.error();
        String taskClz = t.getTaskMethod();
        int index = taskClz.lastIndexOf(".");
        String clzStr = taskClz.substring(0, index);
        String methodStr = taskClz.substring(index + 1, taskClz.length());
        Runnable runnable = null;
        try {
            Class aClass = Class.forName(clzStr);
            Constructor constructor = aClass.getConstructor();
            Object o = constructor.newInstance();
            Method method = aClass.getMethod(methodStr);
            runnable = crmTimingTask.createRunnable(o,method);
        } catch (Exception e) {
            runnable = null;
        }
        if (runnable == null) {
            return AxiosResult.error();
        }
        AsyncManager.execute(runnable);
        return AxiosResult.suc();
    }

    @GetMapping("stop/{taskid}")
    public AxiosResult stopTask(@PathVariable String taskid){
        //停止任务
        boolean b = crmTimingTask.cancelTask(taskid);
        //修改数据库任务状态
        if(b){
            boolean isok = tasksService.stopTask(taskid);
            if(isok)return AxiosResult.suc();
        }
        return AxiosResult.error();
    }

    @GetMapping("start/{taskid}")
    public AxiosResult startTask(@PathVariable String taskid){
        //启动任务
        SysTimingTask t = tasksService.getById(taskid);
        boolean b = crmTimingTask.registrarTask(t);
        //修改数据库任务状态
        if(b){
            boolean isok = tasksService.startTask(taskid);
            if(isok)return AxiosResult.suc();
        }
        return AxiosResult.error();
    }
}
